﻿Public Class frmCajaApertura

    Dim controller As New ControllerSigeco.controller_sistema

    Private _esCierre As Boolean = False

    Public Property esCierre() As Boolean
        Get
            Return _esCierre
        End Get
        Set(ByVal value As Boolean)
            _esCierre = value
        End Set
    End Property

    'Private _codCaja As Integer = 0

    'Public Property idMovimientoCaja() As Integer
    '    Get
    '        Return _codCaja
    '    End Get
    '    Set(ByVal value As Integer)
    '        _codCaja = value
    '    End Set
    'End Property

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()

    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click

        If oiml.idMovimientoCajaActual = 0 Then

            oiml.idMovimientoCajaActual = controller.Insert_Caja_Registro(oiml.dbConn, oiml.idUsuario, fechaIni.Value, Nothing, oiml.idUsuario, _
                                                               oiml.isNumeric(txtMtoinicial.Text), oiml.isNumeric(txtMtoinicial.Text), txtComentario.Text)

            If oiml.idMovimientoCajaActual > 0 Then

                If esCierre Then
                    Me.DialogResult = Windows.Forms.DialogResult.Abort
                    Me.Close()
                Else
                    Me.Close()
                End If

            ElseIf Not controller.MessageError Is Nothing Then
                MessageBox.Show("Error: " & controller.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        ElseIf esCierre And oiml.idMovimientoCajaActual > 0 Then

            txtSaldo.Text = 0

            txtSaldo.Text += oiml.isNumeric(txtBill100.Text) * 100000
            txtSaldo.Text += oiml.isNumeric(txtBill50.Text) * 50000
            txtSaldo.Text += oiml.isNumeric(txtBill20.Text) * 20000
            txtSaldo.Text += oiml.isNumeric(txtBill10.Text) * 10000
            txtSaldo.Text += oiml.isNumeric(txtBill5.Text) * 5000
            txtSaldo.Text += oiml.isNumeric(txtBill2.Text) * 2000

            txtSaldo.Text += oiml.isNumeric(txtMon1000.Text) * 1000
            txtSaldo.Text += oiml.isNumeric(txtMon500.Text) * 500
            txtSaldo.Text += oiml.isNumeric(txtMon100.Text) * 100
            txtSaldo.Text += oiml.isNumeric(txtMon50.Text) * 50

            Dim mtoDiferencia As Decimal = oiml.isNumeric(txtSaldo.Text) - oiml.isNumeric(txtMtoFinal.Text)

            txtSaldo.Text = oiml.isNumeric(txtSaldo.Text).ToString("N0")

            If mtoDiferencia <> 0 Then

                If MessageBox.Show("Error: posee una diferencia de " & mtoDiferencia.ToString("N0") & ", desea continuar y registrar ese saldo?", "CAJA", _
                                   MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If

            End If

            If controller.Update_Caja_Registro(oiml.dbConn, oiml.idMovimientoCajaActual, oiml.idUsuario, fechaIni.Value, DateTime.Now, oiml.idUsuario, _
                                               oiml.isNumeric(txtMtoinicial.Text), oiml.isNumeric(txtSaldo.Text), txtComentario.Text, mtoDiferencia) Then

                oiml.idMovimientoCajaActual = 0
                Me.DialogResult = Windows.Forms.DialogResult.Abort
                Me.Close()

            ElseIf Not controller.MessageError Is Nothing Then

                MessageBox.Show("Error: " & controller.MessageError.Message, "CAJA", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else

            Me.Close()

        End If

    End Sub

    Private Sub Calculo()

        txtSaldo.Text = 0

        txtSaldo.Text += oiml.isNumeric(txtBill100.Text) * 100000
        txtSaldo.Text += oiml.isNumeric(txtBill50.Text) * 50000
        txtSaldo.Text += oiml.isNumeric(txtBill20.Text) * 20000
        txtSaldo.Text += oiml.isNumeric(txtBill10.Text) * 10000
        txtSaldo.Text += oiml.isNumeric(txtBill5.Text) * 5000
        txtSaldo.Text += oiml.isNumeric(txtBill2.Text) * 2000

        txtSaldo.Text += oiml.isNumeric(txtMon1000.Text) * 1000
        txtSaldo.Text += oiml.isNumeric(txtMon500.Text) * 500
        txtSaldo.Text += oiml.isNumeric(txtMon100.Text) * 100
        txtSaldo.Text += oiml.isNumeric(txtMon50.Text) * 50

        txtSaldo.Text += oiml.isNumeric(txtMonCre.Text)
        txtSaldo.Text += oiml.isNumeric(txtMonChe.Text)

        Dim mtoDiferencia As Decimal = oiml.isNumeric(txtSaldo.Text) - oiml.isNumeric(txtMtoFinal.Text)

        txtSaldo.Text = oiml.isNumeric(txtSaldo.Text).ToString("N0")

        txtDif.Text = mtoDiferencia.ToString("N0")

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click

        Me.Close()

    End Sub

    Private Sub btnCierreCaja_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCierreCaja.Click
        If MessageBox.Show("Esta seguro de dar cierre a la caja?", "SIGECO", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
            'idMovimientoCaja = 0
            Me.Size = New Drawing.Size(678, 418)
            Me.grpDetalle.Visible = True
            txtComentario.Enabled = True
            btnCierreCaja.Enabled = False
            esCierre = True
            'Me.DialogResult = Windows.Forms.DialogResult.Abort
            'Me.Close()
        End If
    End Sub

    Private Sub frmCajaApertura_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Size = New Drawing.Size(678, 216)

        If oiml.idMovimientoCajaActual = 0 Then
            txtUsuario.Text = oiml.Usuario
            fechaIni.Value = DateTime.Now
            txtHoraIni.Text = DateTime.Now.Hour.ToString().PadLeft(2, "0") & DateTime.Now.Minute.ToString().PadLeft(2, "0")
            btnCierreCaja.Enabled = False
            btnEliminar.Enabled = False
            Me.grpDetalle.Visible = False
        Else

            Dim dt As DataTable = controller.get_Caja_Datos(oiml.dbConn, oiml.idMovimientoCajaActual)

            If Not dt Is Nothing Then
                txtUsuario.Text = dt.Rows(0)("usuario")
                txtMtoinicial.Text = Decimal.Parse(dt.Rows(0)("montoApertura")).ToString("N0")
                txtMtoFinal.Text = Decimal.Parse(dt.Rows(0)("montoCierre")).ToString("N0")
                txtSaldoEfectivo.Text = Decimal.Parse(dt.Rows(0)("saldoEfectivo")).ToString("N0")
                fechaIni.Value = DateTime.Parse(dt.Rows(0)("fechaApertura"))
                txtHoraIni.Text = DateTime.Parse(dt.Rows(0)("fechaApertura")).Hour.ToString.PadLeft(2, "0") & DateTime.Parse(dt.Rows(0)("fechaApertura")).Minute.ToString.PadLeft(2, "0")
                txtComentario.Text = dt.Rows(0)("comentarios").ToString

                btnCierreCaja.Enabled = True
                btnEliminar.Enabled = True
                txtMtoinicial.Enabled = False
                txtComentario.Enabled = False
            End If

        End If

    End Sub

    Private Sub txtBill100_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBill100.TextChanged
        Calculo()
    End Sub

    Private Sub txtBill50_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBill50.TextChanged
        Calculo()
    End Sub

    Private Sub txtMon1000_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMon1000.TextChanged
        Calculo()
    End Sub

    Private Sub txtMon50_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMon50.TextChanged
        Calculo()
    End Sub

    Private Sub txtMon100_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMon100.TextChanged
        Calculo()
    End Sub

    Private Sub txtMon500_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMon500.TextChanged
        Calculo()
    End Sub

    Private Sub txtBill2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBill2.TextChanged
        Calculo()
    End Sub

    Private Sub txtBill5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBill5.TextChanged
        Calculo()
    End Sub

    Private Sub txtBill10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBill10.TextChanged
        Calculo()
    End Sub

    Private Sub txtBill20_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBill20.TextChanged
        Calculo()
    End Sub

End Class